package cn.wolfcode.wolf2w.destination.service.impl;

import cn.wolfcode.wolf2w.common.core.utils.StringUtils;
import cn.wolfcode.wolf2w.destination.api.domain.Destination;
import cn.wolfcode.wolf2w.destination.api.domain.Region;
import cn.wolfcode.wolf2w.destination.mapper.DestinationMapper;
import cn.wolfcode.wolf2w.destination.mapper.RegionMapper;
import cn.wolfcode.wolf2w.destination.query.DestinationQuery;
import cn.wolfcode.wolf2w.destination.service.IDestinationService;
import cn.wolfcode.wolf2w.destination.service.IRegionService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 目的地Service业务层处理
 * 
 * @author dalang
 * @date 2023-11-21
 */
@Service
@Transactional
public class DestinationServiceImpl extends ServiceImpl<DestinationMapper,Destination> implements IDestinationService {
    @Autowired
    private IRegionService regionService;

    @Override
    public IPage<Destination> queryPage(DestinationQuery qo) {
        IPage<Destination> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        return lambdaQuery()
                .eq(qo.getParentId()!=null,Destination::getParentId,qo.getParentId())
                .and(!StringUtils.isEmpty(qo.getKeyword()),res->res.like(Destination::getName,qo.getKeyword())
                        .or().like(Destination::getEnglish,qo.getKeyword()))
//                .like(!StringUtils.isEmpty(qo.getKeyword()),Destination::getName,qo.getKeyword())
                .eq(qo.getStatus()!=null,Destination::getStatus,qo.getStatus())
                .isNull(qo.getParentId()==null&&qo.getStatus()==null&&StringUtils.isEmpty(qo.getKeyword()),Destination::getParentId)
                .page(page);
    }

    @Override
    public List<Destination> listByRid(Long rid) {
        Region region = regionService.getById(rid);
        String refIds = region.getRefIds();
        String[] split = refIds.split(",");
        List<Long> list = Arrays.stream(split).map(Long::parseLong).collect(Collectors.toList());

        return super.listByIds(list);
    }

    @Override
    public List<Destination> toasts(Long destId) {
        List<Destination> list = getParent(new ArrayList<>(),destId);
        Collections.reverse(list);
        return list;
    }

    @Override
    public IPage<Destination> queryOne(DestinationQuery qo) {
        IPage<Destination> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        return lambdaQuery()
                .page(page);
    }

    @Override
    public List<Destination> searchChildren(Long regionId) {
        List<Destination> list = new ArrayList<>();
        if (regionId == -1L){
            list = lambdaQuery()
                    .eq(Destination::getParentId,1L)
                    .list();
        }else{
            list = this.listByRid(regionId);
        }
        for (Destination dest : list) {
            dest.setChildren(lambdaQuery().eq(Destination::getParentId,dest.getId()).last("limit 5").list());
        }
        return list;
    }

    @Override
    public List<Destination> global() {
        List<Destination> list = lambdaQuery().isNull(Destination::getParentId).list();
        return lambdaQuery().in(Destination::getParentId,list.stream().map(Destination::getId).collect(Collectors.toList())).list();
    }

    private List<Destination> getParent(List<Destination> list,Long destId){
        if (destId == null){
            return list;
        }

        Destination destination = super.getById(destId);
        list.add(destination);

        if (destination.getParentId()!=null) {
            getParent(list,destination.getParentId());
        }

        return list;
    }
}
